package cn.yangtianxia.basic.service.Impl;

import cn.yangtianxia.basic.mapper.BaseMapper;
import cn.yangtianxia.basic.query.BaseQuery;
import cn.yangtianxia.basic.service.IBaseService;
import cn.yangtianxia.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//开启查询事务
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
     BaseMapper<T> mapper;

    @Override
    public List<T> loadAll() {
        return mapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long total = mapper.queryCount(query);
        if (total == null || total == 0){
            return new PageList<>();
        }
            List<T> rows = mapper.queryData(query);
        return new PageList<T>(total,rows);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        mapper.batchRemove(ids);
    }
}
